package com.aquila.datacontrol;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

import android.content.Context;

public class TextReader {
	
//	/**
//	 * 读取标题目录
//	 * @param context
//	 * @return
//	 */
//	public static List<String> readCatalog(Context context){
//		List<String> list = new ArrayList<String>();
//		InputStream in = null;
//		BufferedReader reader = null;
//		try {
//			in = context.getAssets().open("catalog.txt");
//			reader = new BufferedReader(new InputStreamReader(in));
//			String line = null;
//			while((line = reader.readLine())!=null){
//				list.add(line);
//			}
//		} catch (IOException e) {
//			e.printStackTrace();
//		} finally{
//			try {
//				in.close();
//			} catch (IOException e1) {
//				e1.printStackTrace();
//			}
//			try {
//				reader.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return list;
//	}
	
	
	
	/***
	 * 读取章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readNIVChapter(Context context,int bookIndex,int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
		in = context.getAssets().open("niv/"+chapterName);
		reader = new BufferedReader(new InputStreamReader(in));
		int len = in.available();
		char[] buf = new char[len];
		int l = reader.read(buf);
		text = new String(buf,0,l);
		text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		try {
		in.close();
		} catch (IOException e1) {
		e1.printStackTrace();
		}
		try {
		reader.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		return text;
	}
	
	/***
	 * 读取章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readEveryLXChapter(Context context,int bookIndex,int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
		in = context.getAssets().open("every/"+chapterName);
		reader = new BufferedReader(new InputStreamReader(in,"GBK"));
		int len = in.available();
		char[] buf = new char[len];
		int l = reader.read(buf);
		text = new String(buf,0,l);
		text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		try {
		in.close();
		} catch (IOException e1) {
		e1.printStackTrace();
		}
		try {
		reader.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		return text;
	}
	
	
	public static boolean hasXulun(Context context,int bookIndex){
		String chapterName = bookIndex + "_0.txt";
		InputStream in = null;
		try {
		in = context.getAssets().open("every/"+chapterName);
		if (in != null) {
		return true ;
		}
		}catch(Exception e){
		return false ;
		}
		return false;
	}
	
	public static boolean isChapterexist(Context context,int bookIndex,int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		InputStream in = null;
		try {
			in = context.getAssets().open("every/"+chapterName);
			if (in != null) {
            	return true ;
            }
		}catch(Exception e){
			return false ;
		}
		return false;
	}
		/***
	 * 读取章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readLXChapter(Context context,int bookIndex,int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
		in = context.getAssets().open("lx/"+chapterName);
		reader = new BufferedReader(new InputStreamReader(in,"GBK"));
		int len = in.available();
		char[] buf = new char[len];
		int l = reader.read(buf);
		text = new String(buf,0,l);
		text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		try {
		in.close();
		} catch (IOException e1) {
		e1.printStackTrace();
		}
		try {
		reader.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		return text;
	}
	
	/***
	 * 读取和合本章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readAndChapter(Context context,int bookIndex,int chapter){
		DecimalFormat df = new DecimalFormat("00") ;
		String chapterName = df.format(bookIndex) + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
		in = context.getAssets().open("cnbible/"+chapterName);
		reader = new BufferedReader(new InputStreamReader(in));
		int len = in.available();
		char[] buf = new char[len];
		int l = reader.read(buf);
		text = new String(buf,0,l);
		text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
		e.printStackTrace();
		} finally {
		try {
		in.close();
		} catch (IOException e1) {
		e1.printStackTrace();
		}
		try {
		reader.close();
		} catch (IOException e) {
		e.printStackTrace();
		}
		}
		return text;
	}
	
	
	/***
	 * 读取章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readChapter(Context context,int bookIndex,int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
			in = context.getAssets().open("lzz/"+chapterName);
			reader = new BufferedReader(new InputStreamReader(in,"gbk"));
			int len = in.available();
			char[] buf = new char[len];
			int l = reader.read(buf);
			text = new String(buf,0,l);
			text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	/***
	 * 读取中文章节
	 * @param context
	 * @param chapter
	 * @return
	 *2012-7-26 上午10:09:32
	 */
	public static String readCNChapter(Context context,int bookIndex,int chapter){
		String chapterName = SystemConfig.df.format(bookIndex) + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
			in = context.getAssets().open("cnbible/"+chapterName);
			reader = new BufferedReader(new InputStreamReader(in));
			int len = in.available();
			char[] buf = new char[len];
			int l = reader.read(buf);
			text = new String(buf,0,l);
			text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	
	/**
	 * 根据文件名读取文本的内容
	 * @param fileName
	 * @return
	 * String
	 * 2012-10-4 下午1:36:35
	 */
	public static String readFromTxtFile(Context context, String fileName) {
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
			in = context.getAssets().open(fileName);
			reader = new BufferedReader(new InputStreamReader(in));
			int len = in.available();
			char[] buf = new char[len];
			int l = reader.read(buf);
			text = new String(buf,0,l);
//			text = text.replaceAll("\\r\\n","\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				in.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	
	
	/**
	 * 读取一个片段
	 * @param context
	 * @param start
	 * @param end
	 * @param chapter
	 * @return
	 */
	public static String readFragment(Context context,int start,int len,int bookIndex, int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		String text = null;
		InputStream in = null;
		BufferedReader reader = null;
		try {
			in = context.getAssets().open(chapterName);
			reader = new BufferedReader(new InputStreamReader(in));
			reader.skip(start);
			char[] buf = new char[len];
			int length = reader.read(buf);
			text = new String(buf,0,length);
			text = text.replaceAll("\\r\\n","\n");
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return text;
	}
	
	/**
	 * 获取文本字数
	 * @param context
	 * @param bookIndex
	 * @param chapter
	 * @return
	 *2012-7-31 上午10:48:20
	 */
	public static int getContentLength(Context context,int bookIndex, int chapter){
		String chapterName = bookIndex + "_"+chapter+".txt";
		InputStream in = null;
		BufferedReader reader = null;
		int contentLength = -1;
		try{
			in = context.getAssets().open(chapterName);
			reader = new BufferedReader(new InputStreamReader(in));
			int len = in.available();
			char[] buf = new char[len];
			contentLength = reader.read(buf);
			String txt = new String(buf,0,contentLength);
			txt = txt.replaceAll("\\r\\n","\n");
			contentLength = txt.length();
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				in.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return contentLength;
	}
	
//	/***
//	 * 
//	 * @param context
//	 * @return
//	 *2012-7-25 下午3:26:31
//	 */
//	public static String readPostscript(Context context){
//		InputStream in = null;
//		BufferedReader reader = null;
//		StringBuilder sb = new StringBuilder();
//		try{
//			in = context.getAssets().open("postscript.txt");
//			reader = new BufferedReader(new InputStreamReader(in));
//			String line = null;
//			while((line = reader.readLine())!=null){
//				sb.append(line);
//			}
//		}catch(Exception e){
//			e.printStackTrace();
//		}finally{
//			try {
//				in.close();
//			} catch (IOException e1) {
//				e1.printStackTrace();
//			}
//			try {
//				reader.close();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return sb.toString();
//	}
}
